<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * plugins.php
 * Began: Mon January 13 2003
 * 
 * $Id: plugins.php 541 2008-05-20 06:56:16Z rspeicher $
 * 
 ******************************/

define('EQDKP_INC', true);
define('IN_ADMIN', true);
$eqdkp_root_path = './../';
include_once($eqdkp_root_path . 'common.php');

// Start session management
$user->start();
$user->setup();
$user->check_auth('a_plugins_man');

$mode = ( isset($_GET['mode']) ) ? $_GET['mode'] : 'list';
$code = ( isset($_GET['code']) ) ? $_GET['code'] : '';

if ( (!empty($code)) && (!is_dir($eqdkp_root_path . 'plugins/' . $code)) )
{
    message_die($user->lang['error_invalid_plugin']);
}

if ( !empty($code) )
{
    include($eqdkp_root_path . 'plugins/' . $code . '/setup.php');
}

switch ( $mode )
{
    case 'install':
        $plugin = new EQdkp_Plugin($code);
        if ( @is_array($eqdkp_hooks['install']) )
        {
            if ( function_exists($eqdkp_hooks['install'][$code]) )
            {
                if ( $eqdkp_hooks['install'][$code]() )
                {
                    message_die('The plugin was installed successfully.');
                }
            }
        }
        unset($plugin);
        message_die('The plugin failed to install.');
        
        break;
    case 'uninstall':
        $plugin = new EQdkp_Plugin($code);
        if ( @is_array($eqdkp_hooks['uninstall']) )
        {
            if ( function_exists($eqdkp_hooks['uninstall'][$code]) )
            {
                if ( $eqdkp_hooks['uninstall'][$code]() )
                {
                    message_die('The plugin was uninstalled successfully.');
                }
            }
        }
        unset($plugin);
        message_die('The plugin failed to uninstall.');
        
        break;
    case 'list':
        // Open the plugin dir
        if ( $dir = @opendir($eqdkp_root_path . 'plugins/') )
        {
            while ( $file = @readdir($dir) )
            {
                if ( (!is_file($eqdkp_root_path . 'plugins/' . $file)) && (!is_link($eqdkp_root_path . 'plugins/' . $file)) && ($file != '.') && ($file != '..') && ($file != 'CVS') )
                {
                    // Try and include the setup file so we can get install/uninstall hooks
                    $setup_file = $eqdkp_root_path . 'plugins/' . $file . '/setup.php';
                    if ( file_exists($setup_file) )
                    {
                        include($setup_file);
                    }
                    
                    $plugin = new EQdkp_Plugin($plugin_info['code']);
                    if ( @is_array($plugin_info) )
                    {
                        $installed = $plugin->is_installed();
                        
                        $tpl->assign_block_vars('plugins_row', array(
                            'ROW_CLASS' => $eqdkp->switch_row_class(),
                            'NAME' => $plugin_info['name'],
                            'CODE' => $plugin_info['code'],
                            'U_ACTION' => 'plugins.php'.$SID.'&amp;mode=' . ( ($installed) ? 'uninstall' : 'install') . '&amp;code='.$plugin_info['code'],
                            'ACTION' => ( $installed ) ? $user->lang['uninstall'] : $user->lang['install'],
                            'CONTACT' => ( !empty($plugin_info['contact']) ) ? '<a href="mailto:'.$plugin_info['contact'].'">'.$plugin_info['contact'].'</a>' : '&nbsp;')
                        );
                        
                        // Clean up our vars for this plugin, just in case
                        unset($plugin);
                        unset($installed);
                        unset($setup_file);
                        unset($plugin_info);
                    }
                }
            }
        }
        
        $tpl->assign_vars(array(
            'L_NAME' => $user->lang['name'],
            'L_CODE' => $user->lang['code'],
            'L_ACTION' => $user->lang['action'],
            'L_CONTACT' => $user->lang['contact'])
        );
        
        $page_title = sprintf($user->lang['admin_title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['plugins_title'];
        include_once($eqdkp_root_path . 'includes/page_header.php');
        
        $tpl->set_filenames(array(
            'body' => 'admin/plugins.html')
        );
        
        include_once($eqdkp_root_path . 'includes/page_tail.php');
 
        break;
}
?>